// pages/myFiles/myFiles.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    input:0,//鼠标聚焦
    keyword:'', //搜索框输入的关键字
    delete: 0, //管理删除文件
    delfile_id: [], //待删除文件的id数组
    checked_all: 0, //全选

    fileCollection: [],
    filesDisplay: [], //显示的文件
    start: 0, //渲染开始位置
    skipNum: 10, //追加数据数量
    count: 0, //文件总数

  },

  //鼠标聚焦
  focus:function(){
    this.setData({
      input:1
    })
  },

  //鼠标移开
  blur:function(){
    this.setData({
      input:0
    })
  },
  input:function(e){
    this.setData({
      keyword:e.detail.value
    })
  },

  //搜索功能
  search:function(){
    var that = this
    wx.showLoading({
     title: '加载中...',
   })
 
   wx.request({
     url:  'https://www.cerywxr.com/treehole/index.php/Home/ques/search_my_file',
     data: { 
       keyword: that.data.keyword,
       user_id: getApp().globalData.userInfo.user_id
     },
     method: 'POST',
     header: {
       'content-type': 'application/x-www-form-urlencoded'
     },
     success(res) {
       if (res.data.error_code == 0) {
         
         if (res.data.count > 100) {
           that.setData({
             fileCollection: res.data.data,
             count: "99+"
           })
         } else {
           that.setData({
             fileCollection: res.data.data,
             count: res.data.count,
             
           })
         }
         that.setData({
           
           filesDisplay:that.data.fileCollection.slice(0,that.data.skipNum),
           start:that.data.skipNum
         })
       } else if(res.data.error_code == 1){
         wx.showModal({
           title: '提示!',
           content: '搜索内容不得为空',
           showCancel: false,
           success(res) { }
         })
       }
     },
     fail: function (res) {
       wx.showModal({
         title: '哎呀~!',
         content: '网络状态不佳呢！',
         showCancel: false,
         success(res) { }
       })
     },
     complete: function (res) {   //注意！
       wx.hideLoading()
     }
   })
 },

  //页面初始化
  onShow: function (options) {
    wx.showLoading()
    var that = this
    wx.request({
      url: 'https://www.cerywxr.com/treehole/index.php/Home/sharing/get_my_files',
      method: "POST", //请求方式,
      header: {
        'content-type': 'application/x-www-form-urlencoded' //POST提交数据方式
      },
      data: {
        user_id: getApp().globalData.userInfo.user_id,
      },
      success(res) {
        console.log(res.data)
        if (res.data.count > 100) {
          that.setData({
            fileCollection: res.data.data,
            count: "99+"
          })
        } else {
          that.setData({
            fileCollection: res.data.data,
            count: res.data.count
          })
        }
        that.setData({
          filesDisplay: that.data.fileCollection.slice(0, that.data.skipNum),
          start: that.data.skipNum,
        })
        wx.hideLoading()
      }
    })
    
  },

  //触底刷新，获取更多
  onReachBottom: function () {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    var end = that.data.start + that.data.skipNum
    var newData = that.data.fileCollection.slice(that.data.start, end)
    that.setData({
      filesDisplay: that.data.filesDisplay.concat(newData), //拼接数组
      start: end
    })
    setTimeout(() => {
      wx.hideLoading()
    }, 500); //计时器  
  },

  //点击管理
  manage: function () {
    this.setData({
      delete: !this.data.delete,
      checked_all:0
    })
  },

  //选中复选框
  checkboxChange: function (e) {
    this.setData({
      delfile_id: e.detail.value
    })
    console.log(this.data.delfile_id) //以数组形式获取选中文件的id
  },

  //点击全选
  selectAll: function () {
    var that = this
    that.setData({
      checked_all: !that.data.checked_all
    })

    //循环文件给delfile_id赋值
    var allFile=[]
    if(that.data.checked_all==1){//全选打钩
      for (let i = 0; i < that.data.count; i++) {
        allFile[i]=that.data.fileCollection[i].file_id
      }
    }
    that.setData({
      delfile_id: allFile,
    })

    console.log(allFile)
  },

  //点击删除
  deleteFile: function () {
    var that =this
    wx.showModal({
      title: '确定删除选中文件？',
      content: '（一旦删除不可复原）',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')
          //后端删除数据库文件
          wx.request({
            url: 'https://www.cerywxr.com/treehole/index.php/Home/sharing/delete_my_files',
            method: "POST", //请求方式,
            header: {
              'content-type': 'application/x-www-form-urlencoded' //POST提交数据方式
            },
            data: {
              delfile_id: that.data.delfile_id,//提交文件id数组
            },
            success(res) {
              console.log(res.data)
              //删除文件成功以后重新渲染页面
              wx.request({
                url: 'https://www.cerywxr.com/treehole/index.php/Home/sharing/get_my_files',
                method: "POST", //请求方式,
                header: {
                  'content-type': 'application/x-www-form-urlencoded' //POST提交数据方式
                },
                data: {
                  user_id: getApp().globalData.userInfo.user_id,
                },
                success(res) {
                  console.log(res.data)
                  if (res.data.count > 100) {
                    that.setData({
                      fileCollection: res.data.data,
                      count: "99+"
                    })
                  } else {
                    that.setData({
                      fileCollection: res.data.data,
                      count: res.data.count
                    })
                  }
                  that.setData({
                    filesDisplay: that.data.fileCollection.slice(0, that.data.skipNum),
                    start: that.data.skipNum,
                    delete: 0, //管理删除文件
                    delfile_id: []
                  })
                }
              })
            }
          })
        }
      }
    })
  },

})